<?php
if(!isset($_POST['unique'])){
	die('You are not allowed to access the pages');
	exit;
}

$sql = "SELECT * FROM `siteDetails` WHERE `adKey` = '".($_POST['unique'])."' ORDER BY `timePeriod` , `start` ASC";
$jsonReturnArray = array();
//errorEventLog::displaysSQLPhase($sql);
$preSetting = array('0'=>'WEEKDAYS','1'=>'SATURDAY','2'=>'SUNDAY','8'=>'PUBLICHOLIDAY');


$weekday = substr($_POST['weekday'], 0,1); 
$timeInterval = $_POST['timeRange'];
switch($weekday){
	case '0':
		$timeperiod = 2;
	break;
	case '6':
		$timeperiod = 1;
	break;
	case '8':
		$phFlag = 'Y';
		$timeperiod = substr($_POST['weekday'], 1,1); 
	break;
	default:
		$timeperiod = 0;
}

$tRecordSet = $dbObj -> executeQuery($sql);
if($tRecordSet)
{
	$jsonReturnArray['NUMS'] = count($tRecordSet);
	$i=0;
	$j=0;
	$k=0;
	foreach ($tRecordSet as $val)
	{
		$tempTime = $preSetting[$val['TIMEPERIOD']];
		$startTime = formatTimer($val['START']);
		$endTime = formatTimer($val['END']);
		
		$timePeriodRange = $startTime.' - '.$endTime;
		$thisTimePrice = ($val['PRICE'] == 0) ? 'FREE':'$'.$val['PRICE'].'/Hr';
		
		//IF THIS IS THE FLAT PRICE
		$thisTimePrice = ($val['TYPE'] == 3)? 'F$'.$val['PRICE']:$thisTimePrice;
		
		if($val['DURATION'] == -1){
			$thisTimeMaxPark = 'P';
		}elseif (($val['DURATION']/60)>=4){
			$thisTimeMaxPark = 'P';
		}elseif ($val['DURATION'] % 60 ==0){
			$thisTimeMaxPark = ($val['DURATION']/60).'P';
		}else{
			$thisTimeMaxPark = $val['DURATION'].'mins';
		}
		
		//$thisTimeMaxPark = ($val['DURATION'] == -1) ? 'P':(($val['DURATION'] % 60 ==0)?($val['DURATION']/60).'P':$val['DURATION'].'mins');
		
		/******
		 * For highlighing those info within the purposed scope
		 */
		if($timeperiod == $val['TIMEPERIOD']){
			if($val['START'] <= $timeInterval && $val['END'] >= $timeInterval ){
				//different handling required for public holiday
				if($phFlag != 'Y'){
					$timePeriodRange = addDIVContainer($timePeriodRange);
					$thisTimePrice = addDIVContainer($thisTimePrice);
					$thisTimeMaxPark = addDIVContainer($thisTimeMaxPark);
				}else{
					$tempArr['TIME_RANGE'] = addDIVContainer($timePeriodRange);
					$tempArr['PRICE'] = addDIVContainer($thisTimePrice);
					$tempArr['DURATION'] = addDIVContainer($thisTimeMaxPark);
					$marker = $i;
				}
			}
		}
		
		$jsonReturnArray[$tempTime][$i]['TIME_RANGE'] = $timePeriodRange;
		$jsonReturnArray[$tempTime][$i]['PRICE'] = $thisTimePrice;
		$jsonReturnArray[$tempTime][$i]['DURATION'] = $thisTimeMaxPark;
		
		$i++;

	}
	
	/**
	 * If it is the public holiday then add an additional one set of the records for PH
	 */
	if($phFlag == 'Y'){
		$jsonReturnArray[$preSetting['8']] = $jsonReturnArray[$preSetting[$timeperiod]];
		//reset the marker to be the highlighted one
		$jsonReturnArray[$preSetting['8']][$marker] = $tempArr;
	}
	
	$sql = 'SELECT * FROM `'.ADELAIDE_TABLE.'` WHERE `id` = "'.$_POST['unique'].'"';
	$rs = $dbObj ->getOneQuery($sql);
	if($rs){
		$jsonReturnArray['NAME'] = $rs['STREET1'].' '.$rs['SUBURB'];
		$jsonReturnArray['NOTES'] = $rs['NOTE'];
		$jsonReturnArray['TOTAL'] = $rs['TOTAL'];
		$jsonReturnArray['ICONS'] =
		(($rs['SUPPORTTAXI']==0)?'':'taxi-').
		(($rs['SUPPORTBUS']==0)?'':'bus-').
		(($rs['SUPPORTMOTOBIKE']==0)?'':'moto-').
		(($rs['SUPPORTBIKE']==0)?'':'bike-').
		(($rs['SUPPORTNORMAL']==0)?'':'car-').
		(($rs['SUPPORTLOADINGZONE']==0)?'':'car-').
		(($rs['SUPPORTDISABLE']==0)?'':'disable');
	}
}else{
	$jsonReturnArray['NUMS'] = 0;
}

echo json_encode($jsonReturnArray);